package com.whh.parseData.parseXml.anno.entry;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Data
@NoArgsConstructor
public class ElementDesc {
    // xml中元素名称
    private String name;
    // 元素的层级  最外层为0;
    private int layer = -1;
    // 元素的属性 {xml中的名称,bean中的字段名称}
    private Map<String, Field> attributesFieldMap;
    // 是否有子元素
    private boolean isHasChild;
    // 该元素对应的class对象
    private Class clazz;
    // 绝后的子元素，也就是没有子元素的元素
    private Set<String> noHashChildNames;
    // 元素属于那种类型的xml
    private String fileType;
    // 元素对应的方法
    private Method method;
    // 元素对应的Filed
    private Field field;
    // 元素的属性 {xml中的名称,对应的处理方法}
    private Map<String, Method> attributesMethodMap;

    /**
     * 添加元素属性
     */
    public void addAttributeFiled(AttributeDesc attributeDesc) {
        if (this.getAttributesFieldMap() == null) {
            this.setAttributesFieldMap(new HashMap<String, Field>());
        }
        this.getAttributesFieldMap().put(attributeDesc.getName(), attributeDesc.getField());
    }

    /**
     * 获取属性对应的java字段名称
     * @param xmlAtttributeName
     * @return
     */
    public Field getAttributeFiled(String xmlAtttributeName) {
        if (this.getAttributesFieldMap() == null) {
            //System.out.println("该元素的 getAttributesFieldMap为null");
            return null;
        }
        if (this.getAttributesFieldMap().get(xmlAtttributeName) == null) {
            //System.out.println(xmlAtttributeName + " 不存在");
            return null;
        }
        return this.getAttributesFieldMap().get(xmlAtttributeName);
    }

    /**
     * 添加属性method
     */
    public void addAttributeMethod(AttributeDesc attributeDesc) {
        if (this.getAttributesMethodMap() == null) {
            this.setAttributesMethodMap(new HashMap<String, Method>());
        }
        this.getAttributesMethodMap().put(attributeDesc.getName(), attributeDesc.getMethod());
    }

    /**
     * 获取method
     */
    public Method getAttributeMethod(String xmlAtttributeName) {
        if (this.getAttributesMethodMap() == null) {
            System.out.println(this.getFileType() + "_" + this.getName() + " 该元素AttributesMethodMap为空");
            return null;
        }
        if (this.getAttributesMethodMap().get(xmlAtttributeName) == null) {
            System.out.println(xmlAtttributeName + "不存在Method");
            return null;
        }
        return this.getAttributesMethodMap().get(xmlAtttributeName);
    }

    /**
     * 添加那些绝后的子元素名称
     */
    public void addNoHashChildNames(String elementName) {
        if (this.getNoHashChildNames() == null) {
            this.setNoHashChildNames(new HashSet<String>());
        }
        this.getNoHashChildNames().add(elementName);
    }
}
